package start.spring.basic.flowable.service.category.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;

import start.spring.basic.flowable.dao.WfCategoryMapper;
import start.spring.basic.flowable.entity.WfCategory;
import start.spring.basic.flowable.service.BaseServiceImpl;
import start.spring.basic.flowable.service.category.IWfCategoryService;
import start.spring.basic.page.PageQuery;
import start.spring.basic.page.PageResult;
import start.spring.basic.util.common.StringUtil;

/**
 * 流程分类Service业务层处理
 * 
 * @author xuyy
 * @date 2023-08-28
 */
@Service
public class WfCategoryServiceImpl extends BaseServiceImpl implements IWfCategoryService {
    
    @Autowired
    private WfCategoryMapper wfCategoryMapper;

    /**
     * 查询流程分类列表
     * 
     * @param wfCategory 流程分类
     * @return 流程分类
     */
    @Override
    public PageResult<WfCategory> selectWfCategoryPageList(PageQuery pageQuery,WfCategory wfCategory) {
        IPage<WfCategory> page = super.getPage(pageQuery);
        QueryWrapper<WfCategory> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq((wfCategory.getCategoryId()!=null), WfCategory::getCategoryId, wfCategory.getCategoryId());
        queryWrapper.lambda().like(!StringUtil.isEmpty(wfCategory.getCategoryName()), WfCategory::getCategoryName, wfCategory.getCategoryName());
    	queryWrapper.lambda().eq(!StringUtil.isEmpty(wfCategory.getCode()), WfCategory::getCode, wfCategory.getCode());
        queryWrapper.lambda().like(!StringUtil.isEmpty(wfCategory.getRemark()), WfCategory::getRemark, wfCategory.getRemark());
        page = this.wfCategoryMapper.selectPage(page, queryWrapper);
        return super.getPageResult(page);
    }
    
    /**
     * 查询全量流程分类
     * 
     * @param wfCategory 流程分类
     * @return 流程分类
     */
    @Override
    public List<WfCategory> selectWfCategoryList(WfCategory wfCategory) {
        QueryWrapper<WfCategory> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq((wfCategory.getCategoryId()!=null), WfCategory::getCategoryId, wfCategory.getCategoryId());
        queryWrapper.lambda().like(!StringUtil.isEmpty(wfCategory.getCategoryName()), WfCategory::getCategoryName, wfCategory.getCategoryName());
    	queryWrapper.lambda().eq(!StringUtil.isEmpty(wfCategory.getCode()), WfCategory::getCode, wfCategory.getCode());
        queryWrapper.lambda().like(!StringUtil.isEmpty(wfCategory.getRemark()), WfCategory::getRemark, wfCategory.getRemark());
        return this.wfCategoryMapper.selectList(queryWrapper);
    }

    /**
     * 新增流程分类
     * 
     * @param wfCategory 流程分类
     * @return 结果
     */
    @Override
    public int insertWfCategory(WfCategory wfCategory) {
    	wfCategory.setCreateBy(String.valueOf(super.getSysUser().getUserName()));
    	wfCategory.setCreateTime(new Date());
        return this.wfCategoryMapper.insert(wfCategory);
    }

    /**
     * 修改流程分类
     * 
     * @param wfCategory 流程分类
     * @return 结果
     */
    @Override
    public int updateWfCategory(WfCategory wfCategory) {
    	wfCategory.setUpdateBy(String.valueOf(super.getSysUser().getUserName()));
    	wfCategory.setUpdateTime(new Date());
    	UpdateWrapper<WfCategory> updateWrapper = new UpdateWrapper<>();
    	updateWrapper.lambda().eq(WfCategory::getCategoryId, wfCategory.getCategoryId());
    	//非管理员登录
        if(!super.getSysUser().isAdmin()) {
        	updateWrapper.lambda().eq(WfCategory::getCreateBy, super.getSysUser().getUserName());
        }
        return this.wfCategoryMapper.update(wfCategory, updateWrapper);
    }

    /**
     * 批量删除流程分类
     * 
     * @param categoryIds 需要删除的流程分类主键
     * @return 结果
     */
    @Override
    public int deleteWfCategoryByCategoryIds(List<Long> categoryIds) {
    	QueryWrapper<WfCategory> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().in(WfCategory::getCategoryId, categoryIds);
        //非管理员登录
        if(!super.getSysUser().isAdmin()) {
        	queryWrapper.lambda().eq(WfCategory::getCreateBy, super.getSysUser().getUserName());
        }
        return this.wfCategoryMapper.delete(queryWrapper);
    }
	
}
